# 导入库
from py_commander import Network
import time
import threading

target_ip = "192.168.189.128"
command = """
cd D:\\test
python cmd_test.py
"""

# 启动服务器
server_thread = Network.start_server(port=9999)
print("服务器已启动，开始监听消息...")

# 发送预设的命令
print("发送命令...")
response = Network.send_command(target_ip, command, port=9999, timeout=60)
print("执行状态:", response['status'])
print("返回值:", response.get('return_value'))
print("标准输出:", response['stdout'])
print("标准错误:", response['stderr'])

# 创建测试文件并发送
print("创建并发送文件...")
with open('test.txt', 'w') as file:
    file.write('Hello, World!\n')

remote_path = r"D:/test/test.txt"
response = Network.send_file(target_ip, 'test.txt', remote_path, port=9999)
print("文件传输结果:", response)

# 发送消息
print("发送测试消息：测试msg")
Network.send_message(target_ip, "测试msg", port=9999)


while True:
    # 持续监听，接收信息
    msg = Network.get_message()
    time.sleep(0.5)
    
    # 先检查msg是否为None
    if msg is None:
        continue
        
    # 如果msg不是None，则可以安全地访问message字段
    message_content = msg['message']
    print("收到消息：" + message_content)
    
    if message_content == "开始":
        print("start")
    elif message_content == "暂停":
        print("pause")
    elif message_content == "停止":
        print("stop")
        break









